﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Model.EnumEntity;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// NewDeliveryAjax 的摘要说明
    /// </summary>
    public class NewDeliveryAjax : IHttpHandler
    {
        NewDeliveryController controller = new NewDeliveryController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "NewDeliveryState":
                        NewDeliveryState(context);
                        break;
                    case "GetDeliveryType":
                        GetDeliveryType(context);
                        break;
                    default:
                        break;
                }
            }
        }
        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            context.Response.Write(controller.Delete(id));
        }
        private void Update(HttpContext context)
        {
            int ID=Convert.ToInt32(context.Request["id"]);
            int mark = Convert.ToInt32(context.Request["Mark"]);
            string NewsTitle = context.Request["NewsTitle"];
            string NewsContent = context.Request["NewsContent"];
            DateTime StartTime = Convert.ToDateTime(context.Request["StartTime"]);
            DateTime EndTime = Convert.ToDateTime(context.Request["EndTime"]);
            int State = Convert.ToInt32(context.Request["State"]);
            int Priority = Convert.ToInt32(context.Request["Priority"]);
            DateTime CreateTime = Convert.ToDateTime(context.Request["CreateTime"]);
            int DeliveryType = Convert.ToInt32(context.Request["DeliveryType"]);
            NewDeliveryMould model = new NewDeliveryMould()
            {
                ID=ID,
                CommunicationID = mark,
                NewsTitle = NewsTitle,
                NewsContent = NewsContent,
                StartTime = StartTime,
                EndTime = EndTime,
                State = State,
                Priority = Priority,
                CreateTime = CreateTime,
                DeliveryType = DeliveryType
            };
            context.Response.Write(controller.Update(model));
        }
        private void Add(HttpContext context)
        {
            int mark = Convert.ToInt32(context.Request["Mark"]);
            string NewsTitle = context.Request["NewsTitle"];
            string NewsContent = context.Request["NewsContent"];
            DateTime StartTime = Convert.ToDateTime(context.Request["StartTime"]);
            DateTime EndTime =Convert.ToDateTime(context.Request["EndTime"]);
            int State =Convert.ToInt32(context.Request["State"]);
            int Priority =Convert.ToInt32(context.Request["Priority"]);
            DateTime CreateTime =Convert.ToDateTime(context.Request["CreateTime"]);
            int DeliveryType = Convert.ToInt32(context.Request["DeliveryType"]);
            NewDeliveryMould model = new NewDeliveryMould()
            {
                CommunicationID=mark,
                NewsTitle=NewsTitle,
                NewsContent=NewsContent,
                StartTime=StartTime,
                EndTime=EndTime,
                State=State,
                Priority=Priority,
                CreateTime=CreateTime,
                DeliveryType=DeliveryType
            };

            int id = controller.Add(model);
            context.Response.Write(id);
        }
        private void GetList(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int count = 0;
            List<NewDeliveryMouldShow> list = controller.GetList(pageIndex, pageSize, out count);

            for (int i = 0; i < list.Count; i++)
            {
                list[i].NewsContent = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].NewsContent);
            }
            string json = JsonHelper.ListToJson<NewDeliveryMouldShow>(list,count.ToString());
            //string json = JsonHelper.ToJson(list);
            context.Response.Write(json);
        }
        private void NewDeliveryState(HttpContext context)
        {
            List<NewDeliveryState> list = new List<NewDeliveryState>();
            foreach (var item in Enum.GetValues(typeof(EnumNewDeliveryState)))
            {
                NewDeliveryState model = new NewDeliveryState()
                {
                    name = item.ToString(),
                    value = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<NewDeliveryState>(list));
        }
        private void GetDeliveryType(HttpContext context)
        {
            List<DeliveryType> list = new List<DeliveryType>();
            foreach (var item in Enum.GetValues(typeof(EnumDeliveryType)))
            {
                DeliveryType model = new DeliveryType()
                {
                    name = item.ToString(),
                    value = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<DeliveryType>(list));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}